<?php
class Project_Category_Table extends CF_DbTable_Table{

	protected $_name = 'categories';
	static $strBlokEnd = '';
	protected $_primary = 'category_id';

	public function getCategories($limit = null, $category_id = null)
	{
		$select = $this->getAdapter()->select()
		->from($this->_name,
		array('category_id','title','parent_id','has_child'));
		if(!is_null($limit))
		{
			$select->limit($limit);

		}
		if(!is_null($category_id))
		{
			$select->where('category_id=?',$category_id);
		}
		$select->order(array('parent_id','has_child'));
		$resource = $this->getAdapter()->query($select);
		$result = $resource->fetchAll(Zend_Db::FETCH_OBJ);

		return $result;
	}

	public function addCategory(array $categoryData)
	{

		$data = array(
            'title' => $categoryData['title'],
			'parent_id' => $categoryData['parent_id'],
			'has_child' => (int)$categoryData['has_child']
		);
		$this->insert($data);
	}

	public function updateCategory(array $categoryData)
	{
		$category_id = $categoryData['category_id'];
		$data = array();
		var_dump($categoryData);
		exit;
		foreach($categoryData as $attr=>$val){
			switch ($attr){
				case 'title':
					$data[$attr] = $val;
					break;
				case 'parent_id':
					$data[$attr] = (int)$val;
					break;
				case 'has_child':
					$data[$attr] = $val;
					break;
			}
		}
		$category_id = $categoryData['category_id'];

		$this->update($data, 'category_id = '. (int)$category_id);
	}

	public function deleteCategory($category_id)
	{
		if(!empty($category_id)){
			$this->delete('category_id =' . (int)$category_id);
			return true;
		}else{
			return false;
		}

	}


}